package com.PVZ.engine;

import com.PVZ.controller.SoundController;
import com.PVZ.model.Anime;
import com.PVZ.model.Entity;
import com.PVZ.model.emuns.AnimeType;
import com.PVZ.model.plant.CherryBomb;

import java.util.List;

/**
 * Created by @Author: kabuda on @Date: 2021/5/18 19:38
 * <p>
 * 动画控制类 控制动画的播放(切换PNG)
 */
public class AnimeControl {
    /**
     * 播放动画
     *
     * @param entity 需要播放动画的实体类
     */
    public void animePlayer(Entity entity) {
        AnimeType animeType = entity.getCurAnimeType();

        Anime anime = entity.getAnimeMap().get(animeType);

        List<String> animeList = anime.getAnimeList();

        int maxIndex = animeList.size();
        int curIndex = anime.getAnimeIndex();
        if (curIndex < 0) {
            return;
        }

        curIndex++;

        if (animeType == AnimeType.BREAK) {
            if (entity instanceof CherryBomb) {

                if(!entity.hasPlaydeadSound){
                    entity.hasPlaydeadSound = true;
                    entity.getWeapon().setIsBoobed(true);
                    SoundController.playSound("bomb.wav", SoundController.ONCE);
                }

            }
        }

        if (curIndex > maxIndex) {

            //如果是以下类型动画 需要循环播放
            if (animeType == AnimeType.BREAK) {
                entity.setIsAlive(Entity.DEAD);
            } else {
                if (animeType == AnimeType.MOVE && entity instanceof CherryBomb) {
                    entity.setHealth(-1);
                } else {
                    curIndex = 1;
                }

            }
            if (anime.getAnimePlayMode() == 1) {
                curIndex = -1;
                anime.setAnimeIndex(-1);
            }

        }

        if (curIndex >= 0 && curIndex <= maxIndex) {
            anime.setAnimeIndex(curIndex);
            entity.setImgSrc(animeList.get(curIndex - 1));
        }
    }

    /**
     * 切换动画 替换当前动画的图片
     *
     * @param entity    需切换动画的实体
     * @param animeType 切换类型
     */
    public void changeAnime(Entity entity, AnimeType animeType) {

    }
}
